package com.azure.bigstore.controller;

import com.azure.bigstore.entity.Category;
import com.azure.bigstore.entity.Token;
import com.azure.bigstore.service.CategoryService;
import com.azure.bigstore.service.TokenService;
import com.azure.bigstore.utils.CryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by liuhuaibin on 2018/1/28.
 */
@RestController
@RequestMapping("/v1")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private TokenService tokenService;

    /**
     * 获取叶子节点的类别
     * @return
     */
    @GetMapping("/categoryleaf")
    public List<Category> getCategoryLeaf(){
        List<Category> categoryList = categoryService.findAll2Tree();
        List<Category> categoryLeaf = new ArrayList<>();
        for(Category ca : categoryList){
            if(ca.isLeaf() == true){
                ca.setDescr("");
                ca.setGrade(0);
                ca.setPid(0);
                categoryLeaf.add(ca);
            }
        }
        return categoryLeaf;
    }

    /**
     * 树状显示类别
     * @return
     */
    @GetMapping("/categories")
    public List<Category> getCategories(){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String t = request.getHeader("t");// 为空或有值
        System.out.println("tttt:"+t);
        if(t != null && t != ""){
            Token token = tokenService.findByToken(t);
            //未查询到token
            if(token == null ){
                response.setStatus(444);
            }else{
                if(token.getValidityTime().after(new Date())){
                    long time = new Date().getTime()+1800000;//延长30分钟
                    System.out.println("new:"+new Date()+"  time"+new Date(time));
                    token.setValidityTime(new Date(time));
                    tokenService.saveToken(token);
                }else{
                    response.setStatus(444);
                }

            }
        }
        return categoryService.findAll2Tree();
    }

    @GetMapping("/categories/{id}")
    public Category getCategory(@PathVariable String id){
        return categoryService.getCategory(id);
    }

    @PostMapping("/categories")
    public Category saveCategory(Category category){
        System.out.println("===saveCategory:"+category);
        return categoryService.saveCategory(category);
    }

    @DeleteMapping("/categories/{id}")
    public void deleteCategory(@PathVariable String id){
        Category category = categoryService.getCategory(id);
        categoryService.delete(Long.valueOf(id),category.getPid());
    }

}
